/*======================子弹===========================*/
function Bullet(){
	this.init = function(){
		this.ele = document.createElement("div");
		this.ele.className = "bullet";
		document.body.appendChild(this.ele);
		//初始位置
		this.ele.style.left = MyPlane.left() + (MyPlane.width() - this.ele.offsetWidth)/2 + "px";
		this.ele.style.top = MyPlane.top() + "px";
		return this;	
	}
	this.shoot = function(){
		this.t = setInterval(function(){
			this.ele.style.top = this.top() - 10 + "px";
			//****************************碰撞检测*******************
			for(var i in GameEngine.enemies){
				var bulletPos = {x:this.ele.offsetLeft,y:this.ele.offsetTop};
//				console.log(GameEngine.enemies)
				var _left = GameEngine.enemies[i].left() - this.ele.offsetWidth;
				var _right = GameEngine.enemies[i].left() + GameEngine.enemies[i].width();
				var _top = GameEngine.enemies[i].top() - this.ele.offsetHeight;
				var _bottom = GameEngine.enemies[i].top() + GameEngine.enemies[i].height();
				if(bulletPos.x > _left && bulletPos.x < _right && bulletPos.y > _top && bulletPos.y < _bottom){
					this.explode();//子弹爆炸
					GameEngine.enemies[i].hurt();
//					console.log(GameEngine.enemies[i].score)
//					GameEngine.totalscore += GameEngine.enemies[i].score;
				}
			}
			//判断子弹是否出边界
			if(this.top() <  -this.height()){
				clearInterval(this.t);
				this.destroy();
			}
		}.bind(this),30);
	}
	this.destroy = function(){
		this.ele.remove();
	}
	this.explode = function(){
		clearInterval(this.t);
		var prowidth = this.ele.offsetWidth;
		this.ele.className = "bullet_die";
		//设置爆炸位置
		this.ele.style.left =  this.ele.offsetLeft - this.ele.offsetWidth/2 + prowidth/2 + "px";
		var index = 1;
		var t = setInterval(function(){
			if(index >= 3){
				clearInterval(t);
				this.destroy();
				return;
			}
			this.ele.style.background = "url(img/die"+ index +".png)";
			index++;
		}.bind(this),100);
	}
	this.left = function(){
		return this.ele.offsetLeft;
	}
	this.top = function(){
		return this.ele.offsetTop;
	}
	this.width = function(){
		return this.ele.offsetWidth;
	}
	this.height = function(){
		return this.ele.offsetHeight;
	}
}